package com.iyoudoock.citymine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sunmouse.sharelib.ShareUtil;
import com.umeng.debug.log.UMLogUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = MainActivity.class.getSimpleName();
    @BindView(R.id.share_btn)
    Button shareBtn;
    @BindView(R.id.login_btn)
    Button loginBtn;

    private UMShareAPI mShareAPI;
    private ShareUtil mShareUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        this.mShareUtil = ShareUtil.getInstance(this);
        this.mShareAPI = UMShareAPI.get(this);
        this.shareBtn.setOnClickListener(this);
        this.loginBtn.setOnClickListener(this);
        initShare();

    }

    @Override
    public void onClick(View v) {
        if (v == shareBtn) {
            mShareUtil.openShare();
            return;
        }
        if (v == loginBtn) {
            auth();
        }
    }

    /**
     * auth
     */
    private void auth() {
        this.mShareAPI.getPlatformInfo(this, SHARE_MEDIA.WEIXIN, umAuthListener);
    }


    private void initShare() {
        String title = "share";
        String content = "Content:It happened.....";
        String targetUrl = "https://www.baidu.com";
        UMImage image = new UMImage(this, R.mipmap.ic_launcher);

        UMWeb mWeb = new UMWeb(targetUrl);
        mWeb.setTitle(title);
        mWeb.setDescription(content);
        mWeb.setThumb(image);

        mShareUtil.initShare(content, mWeb);
    }

    /**
     *
     */
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Log.v(TAG, "---onStart");
        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            Log.v(TAG, "---onComplete");
            String uid = map.get("uid");
            String accessToken = map.get("accessToken");
            String name = map.get("name");
            String avatar = map.get("iconurl");
            String sex = map.get("sex");

        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            Log.v(TAG, "---onError");

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Log.v(TAG, "---onCancel");

        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareUtil.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mShareUtil.release();
    }
}
